package updown.util;

import cn.hutool.extra.ssh.JschUtil;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import lombok.extern.slf4j.Slf4j;
import updown.domain.ServerHost;

import java.util.concurrent.TimeUnit;

/**
 * <p>Title: ww.kaixin.downupservice.util </p>
 * <p>Description:to-22-6-1: -  </p>
 *
 * @Author:jiangkaixin
 * @Date:2021/12/23
 * @Version: 1.0
 */
@Slf4j
public class SshUtil {

    /**
     * 获取连接 session
     *
     * @param sshModel
     * @return
     */
    public static Session getSession(ServerHost sshModel) {
        Session session;
        session = JschUtil.openSession(sshModel.getHost(), sshModel.getPort(), sshModel.getUser(), sshModel.getPassword());
        try {
            session.setServerAliveInterval((int) TimeUnit.SECONDS.toMillis(500000));
            session.setServerAliveCountMax(20);
        } catch (JSchException ignored) {
            log.info("getSession(),获取链接失败>>>:{}", ignored);
            return session;
        }
        log.info("{}:获取session成功",sshModel);
        return session;
    }
}
